package com.jakewharton.rxbinding4.component;

import static com.jakewharton.rxbinding4.internal.Preconditions.checkNotNull;

import com.jakewharton.rxbinding4.InitialValueObservable;

import io.reactivex.rxjava3.annotations.CheckReturnValue;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;
import ohos.agp.components.AbsButton;

/**
 * Static factory methods for creating {@linkplain Observable observables} and {@linkplain Consumer
 * actions}* for {@link AbsButton}.
 */
public final class RxAbsButton {
    /**
     * Create an observable of booleans representing the checked state of {@code view}.
     * <p>
     * <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
     * to free this reference.
     * <p>
     * <em>Warning:</em> The created observable uses {@link AbsButton#setCheckedStateChangedListener}
     * to observe checked changes. Only one observable can be used for a view at a time.
     * <p>
     * <em>Note:</em> A value will be emitted immediately on subscribe.
     *
     * @param view view
     * @return the initial value observable
     */
    @CheckReturnValue
    @NonNull
    public static InitialValueObservable<Boolean> checkedStateChanges(@NonNull AbsButton view) {
        checkNotNull(view, "view == null");
        return new AbsButtonCheckedStateChangedObservable(view);
    }

    /**
     * An action which sets the checked property of {@code view}.
     * <p>
     * <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
     * to free this reference.
     *
     * @param view view
     * @return the consumer
     */
    @CheckReturnValue
    @NonNull
    public static Consumer<? super Boolean> checked(AbsButton view) {
        checkNotNull(view, "view == null");
        return view::setChecked;
    }

    /**
     * An action which sets the toggles property of {@code view} with each value.
     * <p>
     * <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
     * to free this reference.
     *
     * @param view view
     * @return the consumer
     */
    @CheckReturnValue
    @NonNull
    public static Consumer<? super Object> toggle(@NonNull AbsButton view) {
        checkNotNull(view, "view == null");
        return value -> view.toggle();
    }

    private RxAbsButton() {
        throw new AssertionError("No instances.");
    }
}
